package com.jyhs.maven.servlet;

import com.alibaba.fastjson.JSON;
import com.jyhs.maven.entity.Menu;
import com.jyhs.maven.service.MenuService;
import com.jyhs.maven.service.impl.MenuServiceImpl;
import com.jyhs.maven.util.ReflectUtil;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;


//@WebServlet("/menu")
public class MenuServlet extends HttpServlet {


    private MenuService menuService = new MenuServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String opr = req.getParameter("opr");
            switch (opr) {
                case "add":
                    addMenu(req, resp);
                    break;
                case "del":
                    deleteMenu(req, resp);
                    break;
                case "edit":
                    editMenu(req, resp);
                    break;
                default:
                    selectMenu(req, resp);
                    break;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public void selectMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(menuService.selectAll()));
        writer.flush();
        writer.close();

    }

    public void addMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ParseException, IllegalAccessException {
        PrintWriter writer = resp.getWriter();
        Menu menu = new Menu();
        ReflectUtil.httpServletRequestToObject(req, menu);
        writer.print(JSON.toJSONString(menuService.insert(menu)));
        writer.flush();
        writer.close();

    }

    public void deleteMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Long id = Long.parseLong(req.getParameter("id"));
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(menuService.delete(id)));
        writer.flush();
        writer.close();
    }

    public void editMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, ParseException, IllegalAccessException {

        PrintWriter writer = resp.getWriter();
        Menu menu = new Menu();
        ReflectUtil.httpServletRequestToObject(req, menu);
        writer.print(JSON.toJSONString(menuService.update(menu)));
        writer.flush();
        writer.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
